package com.mes.basicdata.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.context.annotation.Lazy;

import com.mes.common.domain.BasicModel;
/**
 * @time 2017/3/15
 * @author SnowBear
 *
 */
@Entity()
@Table(name = "B_CLASSINFO")
@BatchSize(size=30)
@Lazy(value=true)
public class ClassInformation extends BasicModel implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public final static String CLASSTYPE_CLASS = "1"; // 此分类结点为维度
	
	public final static String CLASSTYPE_DIMENSION = "2"; // 此分类结点为维度
	@Id
	@GenericGenerator(name = "uuid", strategy = "assigned")
	@Column(name="UUID",scale=0)
	private String uuid;

	// 类别号
	@Column(name="CLASSID")
	private String classId;

	// 类别名称
	@Column(name="CLASSNAME")
	private String className;
	
	// 父类别
	@Column(name="PARENTID",length=50)
	private String parentClassId;
	
	// 父类别唯一号
	@Column(name="PARENTUID", length=50)
	private String parentClassUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="PARENTUID",insertable=false,updatable=false)
	private ClassInformation parentClass;
	

	// 分类的组别
	// 同一个Sort内名称不允许重复
	// sort究竟是否是一个分类我还未想好
	@Column(name="SORTID", length=50)
	private String sortId;
	
	// 是否有效
	@Column(name="ISINEFFECT",precision=2 ,scale=0)
	private Boolean effective;
	
	// 类别顺序
	@Column(name="SORTINDEX",precision=5 ,scale=0)
	private Integer classIndex;
	
	// 映射到其它分类中的唯一号
	@Column(name="MAPKEYUID",length=50)
	private String mapKeyUid;
	
	// 分类类型，是否为虚节点：1-否，2-是
	@Column(name="CLASSTYPE",length=50)
	private String classType;
	
	// 客户自定义分类编号
	@Column(name="CUSTOMER_CLASSID",length=50)
	private String customerClassId;

	// 是否是叶子节点
	@Column(name="LEAFNODE",precision=5 ,scale=0)
	private Boolean leafNode;
	
	// Tree的Label信息
	@Transient
	private String treeLabel;
	// 节点层次
	@Transient
	private int ClassLevel;
	
	// 该结点的所有子节点
//	@OneToMany(fetch=FetchType.EAGER)
//	@JoinColumn(name = "PARENTUID")	
	@OneToMany(fetch=FetchType.LAZY, mappedBy = "parentClass")
	private List<ClassInformation> children;	
	
	@Column(name="CHILDSEQ",precision=10, scale=0)
	private Integer childSeq;
	
	// 映射的领域对象类管理类的名称
	@Column(name="MANAGER_CLASS_NAME",length=255)
	private String managerClassName;

	// 映射的领域对象类对应的编码属性名称
	@Column(name="CODE_ATTRIBUTE",length=50)
	private String codeAttribute;

	// 映射的领域对象类对应的编码属性名称
	@Column(name="OBJECT_CLASS_NAME",length=50)
	private String classAttribute;

	// 本分类对应的图标路径
	@Column(name="ICONPATH",length=50)
	private String iconPath;

	// 对象类名称
	@Transient
	private String objectClassName;

	@Column(name="CUSTOMER_FEATURES_FORMAT")
	protected String customerFeaturesFormat;


	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.uuid;
	}

	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.uuid = uuid;
	}
	
	public Boolean getEffective() {
		return effective;
	}

	public void setEffective(Boolean effective) {
		this.effective = effective;
	}

	public String getClassId()
	{
		return classId;
	}

	public void setClassId(String classId)
	{
		this.classId = classId;
		this.setClassLevel(functionClassLevel(classId));
	}

	public String getClassName()
	{
		return className;
	}

	public void setClassName(String className)
	{
		this.className = className;
	}

//	public ClassInformation getParentClass()
//	{
//		return parentClass;
//	}
//
//	public void setParentClass(ClassInformation parentClass)
//	{
//		this.parentClass = parentClass;
//	}

	public Integer getClassIndex()
	{
		return classIndex;
	}

	public void setClassIndex(Integer classIndex)
	{
		this.classIndex = classIndex;
	}

	public String getParentClassId()
	{
		return parentClassId;
	}

	public void setParentClassId(String parentClassId)
	{
		this.parentClassId = parentClassId;
	}

	public String getSortId()
	{
		return sortId;
	}

	public void setSortId(String sortId)
	{
		this.sortId = sortId;
	}

	public String getParentClassUid()
	{
		return parentClassUid;
	}

	public void setParentClassUid(String parentClassUid)
	{
		this.parentClassUid = parentClassUid;
	}

	public String getMapKeyUid()
	{
		return mapKeyUid;
	}

	public void setMapKeyUid(String mapKeyUid)
	{
		this.mapKeyUid = mapKeyUid;
	}

	public String getClassType()
	{
		return classType;
	}

	public void setClassType(String classType)
	{
		this.classType = classType;
	}

	public String getCustomerClassId()
	{
		return customerClassId;
	}

	public void setCustomerClassId(String customerClassId)
	{
		this.customerClassId = customerClassId;
	}

	public Boolean getLeafNode()
	{
		return leafNode;
	}

	public void setLeafNode(Boolean leafNode)
	{
		this.leafNode = leafNode;
	}

	public int getClassLevel()
	{
		return ClassLevel;
	}

	public void setClassLevel(int classLevel)
	{
		ClassLevel = classLevel;
	}

	private int functionClassLevel(String classId)
	{
		String[] para = classId.split(".");
		return para.length;
	}

	public String getTreeLabel()
	{
		return this.classId + "(" + this.className + ")";
	}

	public void setTreeLabel(String treeLabel)
	{
		this.treeLabel = treeLabel;
	}

	public List<ClassInformation> getChildren()
	{
		return children;
	}

	public void setChildren(List<ClassInformation> children)
	{
		this.children = children;
	}

	public Integer getChildSeq()
	{
		return childSeq;
	}

	public void setChildSeq(Integer childSeq)
	{
		this.childSeq = childSeq;
	}

	public String getManagerClassName()
	{
		return managerClassName;
	}

	public void setManagerClassName(String managerClassName)
	{
		this.managerClassName = managerClassName;
	}

	public String getIconPath()
	{
		return iconPath;
	}

	public void setIconPath(String iconPath)
	{
		this.iconPath = iconPath;
	}

	public String getCodeAttribute()
	{
		return codeAttribute;
	}

	public void setCodeAttribute(String codeAttribute)
	{
		this.codeAttribute = codeAttribute;
	}

	public String getObjectClassName()
	{
		return objectClassName;
	}

	public void setObjectClassName(String objectClassName)
	{
		this.objectClassName = objectClassName;
	}

	public String getClassAttribute()
	{
		return classAttribute;
	}

	public void setClassAttribute(String classAttribute)
	{
		this.classAttribute = classAttribute;
	}

	public String getCustomerFeaturesFormat()
	{
		return customerFeaturesFormat;
	}

	public void setCustomerFeaturesFormat(String customerFeaturesFormat)
	{
		this.customerFeaturesFormat = customerFeaturesFormat;
	}
/*	<set name="children" inverse="true" lazy="true"
			cascade="none">
			<key>
				<column name="PARENTUID" not-null="true" />
			</key>
			<one-to-many
				class="com.mes.basicdata.persistence.domain.ClassInformation" />
		</set>
		<set name="attributes" table="B_CLASS_ATTRIBUTE_RELATION"
			inverse="true" lazy="true" cascade="none"
			order-by="ATTRIBUTE_INDEX">
			<key>
				<column name="CLASS_UID" not-null="true" />
			</key>
			<many-to-many
				class="com.mes.basicdata.persistence.domain.extend.Attribute"
				column="ATTRIBUTE_UID" />
		</set>
		<set name="attributeRelations" inverse="true" lazy="true"
			cascade="none" order-by="ATTRIBUTE_INDEX">
			<key>
				<column name="CLASS_UID" not-null="true" />
			</key>
			<one-to-many
				class="com.mes.basicdata.persistence.domain.extend.ClassAttributeRelation" />
		</set>*/
}
